<template>
  <el-container>
    <el-aside :width="asideWidth">
      <div class="cl-nav__logo">
        <div class="logo_image">
          <slot name="logo">logo</slot>
        </div>
        <div class="logo_name" v-show="!isCollapse">
          <slot name="name"></slot>
        </div>
      </div>
      <div class="cl-nav__head">
        <div class="header_image">
          <img :src="headImage" class="img" :style="headerImageStyle">
        </div>
        <p class="header_name" v-show="!isCollapse"><slot name="user">admin</slot></p>
      </div>
      <el-menu
        class="cl-nav__menu"
        unique-opened
        :default-active="menuActiveName"
        :default-openeds="menuOpenName"
        :collapse="isCollapse"
        @open="handleOpen"
        @close="handleClose"
        @select="handleSelect"
        router
      >
        <template v-for="(item, idx) in menuData">
          <el-submenu class="menu_item" v-if="item.children" :index="item.menuCode" :key="idx">
            <template slot="title">
              <i :class="['iconfont', item.menuUrl]"></i>
              <span slot="title">{{ item.menuName }}</span>
            </template>
            <el-menu-item v-for="(citem, i) in item.children" :key="i" :index="citem.menuCode" :route="citem.route">
              <i :class="['iconfont', citem.menuUrl]"></i>
              <span slot="title">{{ citem.menuName}}</span>
            </el-menu-item>
          </el-submenu>
          <el-menu-item class="menu_item" v-else :index="item.menuCode" :key="idx" :route="item.route">
            <i :class="['iconfont', item.menuUrl]"></i>
            <span slot="title">{{ item.menuName }}</span>
          </el-menu-item>
        </template>
      </el-menu>
      <div class="cl-nav__footer"></div>
      <div
        class="cl-nav__toggle_btn"
        :class="{'btn_collapse': isCollapse}"
        @click="handleToggleView"
      ><i class="el-icon-caret-left"></i></div>
    </el-aside>
    <el-container>
      <el-header class="cl-nav__header">
        <p class="cl-nav__breadcrumb" ref="breadcrumb">{{ breadCrumb | strBreadCrumb }}</p>
        <div class="center_container">
          <p class="warning-message" ref="slideMsg" :style="{'left': slideLeft}">{{ slideMessage }}</p>
        </div>
        <div class="right_group">
          <slot name="feature"><div><i class="iconfont"></i><span>退出</span></div></slot>
        </div>
      </el-header>
      <el-main>
        <slot>
          <div> content </div>
        </slot>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { Container, Aside, Header, Main, Menu, Submenu, MenuItem, MenuItemGroup } from 'element-ui'

export default {
  name: 'NavMenu',
  components: { 
    Container, 
    Aside, 
    Header, 
    Main,
    'el-menu': Menu, 
    'el-submenu': Submenu, 
    'el-menu-item': MenuItem, 
    'el-menu-item-group': MenuItemGroup
  },
  filters: {
    strBreadCrumb (arr) {
      return arr.join(' >> ')
    }
  },
  props: {
    headImage: {
      type: String,
      default: ''
    },
    menuData: {
      type: Array,
      default () {
        return []
      }
    },
    breadCrumb: {
      type: Array,
      default (nv) {
        return []
      }
    },
    slideMessage: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isCollapse: false,
      menuActiveName: 'JCZL',
      menuOpenName: [],
      slideLeft: '100%',
      onOffSlide: false
    }
  },
  computed: {
    asideWidth () {
      return this.isCollapse? '50px' : '180px'
    },
    headerImageStyle () {
      if (this.isCollapse) {
        return {
          width: '32px',
          height: '32px',
          margin: '20px auto'
        }
      } else {
        return {
          width: '52px',
          height: '52px',
          margin: '10px auto'
        }
      }
    }
  },
  mounted () {
    this.menuActiveName = this.$route.name
    this.menuOpenName = [this.$route.name.split('_')[0]]
    this.$emit('select', { index:this.$route.name.split('_')[0], indexPath: [this.$route.name.split('_')[0], this.menuActiveName] })
  },
  methods: {
    handleOpen () {

    },
    handleClose () {

    },
    handleToggleView () {
      this.isCollapse = !this.isCollapse
      window.dispatchEvent(new Event('resize'))
    },
    handleSelect (index, indexPath) {
      this.menuActiveName = index
      let arrIdx = index.split('_')
      this.menuOpenName = [arrIdx[0]]
      this.$emit('select', { index, indexPath })
    },
    showSlideTip (speed) {
      this.onOffSlide = true
      let elem = this.$refs.slideMsg
      let elemWidth = elem.offsetWidth
      speed = speed || 1
      let _vm = this
      function animation () {
        let left = elem.offsetLeft
        if (left - speed < -elemWidth) {
          _vm.slideLeft = '100%'
        } else {
          _vm.slideLeft = left - speed + 'px'
        }
        if (_vm.onOffSlide) {
          requestAnimationFrame(animation)
        }
      }
      animation()
    },
    hideSlideTip () {
      this.onOffSlide = false
      let _vm = this
      let elem = this.$refs.slideMsg
      function animation () {
        let opacity = elem.style.opacity || 1
        if (opacity > 0) {
          elem.style.opacity = opacity - 0.01
          requestAnimationFrame(animation)
        } else {
          elem.style.opacity = 1
          _vm.slideLeft = '100%'
        }
      }
      animation()
    }
  }
}
</script>

<style lang="scss">
.el-container {
  height: 100%;
}
.el-aside {
  position: relative;
  height: 100%;
  overflow: initial;
  box-shadow: 2px 0 12px 0 rgba(32, 65, 145, 0.22);
  z-index: 1;
}
.cl-nav__logo {
  color: #fff;
  height: 50px;
  line-height: 50px;
  overflow: hidden;
  background:linear-gradient(144deg,rgba(54,99,246,1) 0%,rgba(67,149,250,1) 100%);
  .logo_image {
    float: left;
    width: 50px;
    height: 50px;
    background-color: rgba(48, 102, 229, 1);
  }
  .logo_name {
    font-size: 24px
  }
}
.cl-nav__head {
  border-bottom: 1px solid rgba(234, 235, 240, 1);
  .header_image .img {
    display: block;
  }
  .header_name {
    margin-top: -4px;
    margin-bottom: 10px;
    color: rgba(135, 138, 146, 1);
    font-size: 12px;
    text-align: center;
    line-height: 17px;
  }
}
.cl-nav__toggle_btn {
  $bcolor: rgba(48, 131, 242, 1);
  position: absolute;
  right: -15px;
  top: 50%;
  width: 15px;
  height: 80px;
  margin-top: -40px;
  color: #fff;
  font-size: 16px;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
  background-color: $bcolor;
  z-index: 1;
  cursor: pointer;
  &.btn_collapse .el-icon-caret-left {
    transform: rotate(180deg);
  }
  .el-icon-caret-left {
    margin-top: 32px;
  }
  &::before {
    position: absolute;
    top: -6px;
    left: 0;
    width: 6px;
    height: 6px;
    content: '';
    display: block;
    background: -webkit-radial-gradient(100% 0%, transparent 70%, $bcolor 70%);
  }
  &::after {
    position: absolute;
    bottom: -6px;
    left: 0;
    width: 6px;
    height: 6px;
    content: '';
    display: block;
    background: -webkit-radial-gradient(100% 100%, transparent 70%, $bcolor 70%);
  }
}
.cl-nav__menu {
  padding: 20px 0;
  max-height: calc(100% - 150px);
  border-right: none 0;
  overflow-y: auto;
  &.el-menu--collapse {
    width: 50px;
    .iconfont {
      margin: 0 !important;
    }
    .el-submenu__title, .el-menu-item, .el-tooltip {
      text-align: center;
      padding-left: 13px !important;
      padding-right: 13px !important;
    }
    .menu_item:nth-child(4n)::after {
      left: 10px;
      right: 10px;
    }
  }
  .menu_item {
    .iconfont {
      margin-right: 4px;
    }
    &:nth-child(4n) {
      position: relative;
      margin-bottom: 20px;
      &::after {
        display: block;
        content: '';
        position: absolute;
        left: 20px;
        right: 0;
        bottom: -10px;
        height: 1px;
        background-color: #EAEBF0;
      }
    }
    &:last-child {
      position: relative;
      margin-bottom: 20px;
      &::after {
        display: block;
        content: '';
        position: absolute;
        left: 20px;
        right: 0;
        bottom: -10px;
        height: 1px;
        background-color: #ffffff;
      }
    }
  }
  >.el-menu-item.is-active {
    border-left: 2px solid rgba(48, 131, 242, 1);
    background-color: rgba(234, 235, 240, 1);
  }
  .is-active .el-submenu__title {
    color: rgba(48, 131, 242, 1);
    border-left: 2px solid rgba(48, 131, 242, 1);
    background-color: rgba(234, 235, 240, 1);
    i {
      color: rgba(48, 131, 242, 1);
    }
  }
  .el-menu-item {
    height: 40px !important;
    line-height: 40px !important;
    min-width: 50px;
  }
  .el-submenu__title {
    height: 40px !important;
    line-height: 40px !important;
  }
}
.el-menu--popup .el-menu-item {
  height: 40px;
  line-height: 40px;
}

.cl-nav__header {
  position: relative;
  height: 50px !important;
  line-height: 50px;
  background-color: #4B4F58 !important;
  .cl-nav__breadcrumb {
    float: left;
    margin: 0 20px 0 0;
    color: #C3C6D6;
    font-size: 12px;
    line-height: 50px;
  }
  .center_container {
    position: relative;
    height: 50px;
    line-height: 50px;
    color: #FAAA1B;
    font-size: 12px;
    margin-right: 470px;
    overflow: hidden;
  }
  .warning-message {
    position: absolute;
    left: 100%;
    top: 0;
    white-space: nowrap;
  }
  .el-breadcrumb {
    float: left;
    margin-left: 24px;
    margin-right: 20px;
  }
  /deep/ .el-breadcrumb__inner, .el-breadcrumb__separator {
    color: #C3C6D6;
    font-size: 12px;
    line-height: 50px;
  }
  /deep/ .el-breadcrumb__item:last-child .el-breadcrumb__inner {
    color: #fff;
  }
  /deep/ .el-breadcrumb__item {
    height: 50px;
    line-height: 50px;
  }
  .right_group {
    position: absolute;
    right: 0;
    top: 0;
    display: flex;
    color: #C3C6D6;
    line-height: 50px;
    &>div {
      position: relative;
      padding: 0 20px;
      font-size: 12px;
      line-height: 50px;
      cursor: pointer;
      &:not(:last-child)::after {
        position: absolute;
        top: 20px;
        right: 0;
        content: '';
        display: block;
        width: 1px;
        height: 10px;
        background-color: #C3C6D6;
      }
    }
    .iconfont {
      margin-right: 6px;
    }
    .more_btn {
      position: relative;
      .sub_ul {
        position: absolute;
        right: 0;
        top: 50px;
        padding: 5px 0;
        display: none;
        background-color: #2A2D31;
        z-index: 999;
        li {
          height: 30px;
          width: 150px;
          text-align: center;
          color: #fff;
          font-size: 14px;
          line-height: 30px;
          &:hover {
            background-color: #1f2225;
          }
        }
      }
      &:hover {
        background-color: #3B3F45;
      }
      &:hover .sub_ul {
        display: block;
        z-index: 99;
      }
    }
  }
}
.el-header {
  background-color: #aaa;
}
</style>
